<template>
  <el-select v-model="selectVal"
             filterable
             placeholder="请选择"
             @change="change"
             :disabled="disabled"
             clearable>
    <el-option v-for="(item,index) in selectArr"
               :key="index"
               :label="item.roomName"
               :value="item.roomCode">
    </el-option>
  </el-select>
</template>

<script>
import { getStorageSetList } from '@/api/storageSet/storageSet' //库房来源于库房管理
export default {
  model: {
    prop: 'name',
    event: 'select'
  },
  name: 'selectAttr',
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    name: '',
    row:{
        type:Object
    }
  },
  data() {
    return {
      infoName: '',
      selectArr: []
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    loadData() {
      getStorageSetList({id:this.row.warehouseId}).then(res => {
        let data = res.data.aaData
        for (let i = 0; i < data.length; i++) {
          if (data[i].warehouseId === this.row.warehouseId) {
            this.selectArr.push({
              roomId: data[i].id,
              roomCode: data[i].roomCode,
              roomName: data[i].roomName
            })
          }
        }
      })
    },
    change(val) {
      this.$emit('select', val)
      for (let i = 0; i < this.selectArr.length; i++) {
        if (this.selectArr[i].roomCode === val) {
          this.$emit('selectId', this.selectArr[i].roomId)
          this.$emit('selectName', this.selectArr[i].roomName)
        }
      }
    }
  },
  computed: {
    selectVal: {
      set(val) {
        let data = ''
        for (let i = 0; i < this.selectArr.length; i++) {
          if (val === this.selectArr[i].roomCode) {
            data = this.selectArr[i]
          }
        }
        this.$emit('change', data)
        this.$emit('select', val)
      },
      get() {
        return this.name
      }
    }
  },
  watch: {
    name(val) {
      this.infoName = val
    }
  }
}
</script>

<style scoped>
</style>
